$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/order/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
            { label: '订单号', name: 'billno', index: 'billNo', width: 80 },
            { label: '用户名', name: 'username', index: 'userName', width: 80 },
			{ label: '创建时间', name: 'createdate', index: 'createDate', width: 80 },
			{ label: '订单状态', name: 'orderstatus', index: 'orderStatus', width: 80 ,formatter: function(value, options, row){
                if(value === "unpaid"){
                    return '<span class="label label-primary">待付款</span>';
                }
                if(value === "beforedeliver"){
                    return '<span class="label label-success">待发货</span>';
                }
                if(value === "delivering"){
                    return '<span class="label label-warning">待收货</span>';
                }
                if(value === "uncommented"){
                    return '<span class="label label-warning">待评价</span>';
                }
                if(value === "success"){
                    return '<span class="label label-warning">已完成</span>';
                }
                if(value === "refund"){
                    return '<span class="label label-warning">待退款</span>';
                }
                if(value === "refundsuccess"){
                    return '<span class="label label-warning">退款完成</span>';
                }
                if(value === "rejectfund"){
                    return '<span class="label label-warning">退款拒绝</span>';
                }
            } },
			{ label: '支付时间', name: 'paydate', index: 'payDate', width: 80 }, 			
			{ label: '支付方式', name: 'paytype', index: 'payType', width: 80 }, 			
			{ label: '金额', name: 'totalfee', index: 'totalFee', width: 80, formatter: function(value, options, row){
                return value / 100 }
            },
			{ label: '物流类型', name: 'wuliutype', index: 'wuliuType', width: 80 },
			{ label: '物流订单号', name: 'wuliuorderno', index: 'wuliuOrderNo', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            billno : null
		},
		address:null,
		list:null,
		showList: true,
		showList2: true,
		title: null,
		order: {
            billno : null,
            orderstatus : null,
		},
        sels: [
            {value:"unpaid",week:"待付款订单"},
            {value:"beforedeliver",week:"待发货"},
            {value:"delivering",week:"待收货订单"},
            {value:"uncommented",week:"待评价"},
			{value:"success",week:"已完成"},
			{value:"refund",week:"待退款"},
			{value:"refundsuccess",week:"退款完成"},
			{value:"rejectfund",week:"拒绝退款"}
        ],
        wulius: [],
	},
    created: function () {
        $.ajax({
            url: baseURL + 'sys/expresscode/foundList',
            type: "POST",
            contentType: "application/json",
            success: function (result) {
                vm.wulius = result.data
            },
            error: function (error) {
                alert('数据加载失败!');
            }
        })
    },
	methods: {

		query: function () {
			vm.reload();
		},
        ship: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "发货";
            vm.getInfo(id)
        },
		fahuo:function () {
            $.ajax({
                type: "POST",
                url: baseURL + "sys/order/fahuo",
                contentType: "application/json",
                data: JSON.stringify(vm.order),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.order = {};
		},
        tuihuo: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList2 = false;
            vm.title = "退货";
            
            vm.getInfo(id)
		},
        cateName: function () {
            vm.showList = true;
            vm.showDom = 'list'
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'orderstatus': this.order.orderstatus
                },
                page: page
            }).trigger("reloadGrid");
        },
		saveOrUpdate: function (event) {
			var url = vm.order.id == null ? "sys/order/save" : "sys/order/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.order),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
        getSelect:function(_string){
			var returnText= '';
			var _arr = JSON.parse(_string);

			for (var i = 0 ; i < _arr.length ; i++){
                returnText += _arr[i].propertyname + ':'+ _arr[i].propertyvalue
			}

			return returnText
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/order/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
        exportExcel:function () {
            window.location.href=baseURL+"sys/order/excel?orderstatus="+this.order.orderstatus;
        },
		getInfo: function(id){
			$.get(baseURL + "sys/order/info/"+id, function(r){
                vm.order = r.order;
                vm.address = r.address;
                vm.list = r.list;
            });
		},
		reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'billno': vm.q.billno},
                page:page
            }).trigger("reloadGrid");
		}
	}
});